package com.goddess.yeecolor.webview;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.CookieUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.view.sharePopuWindow;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.sharesdk.framework.ShareSDK;

/**
 * 作者: CaoJunping
 * 时间: 2017/2/9
 * 作用: ******
 * 备注: 雅值WebView
 */
public class YouYaLevelWebView extends BaseActivity {
    @InjectView(R.id.title_content)
    TextView titleContent;
    @InjectView(R.id.title_toobar)
    Toolbar titleToobar;
    @InjectView(R.id.webview)
    WebView webview;
    private View view ;
    private sharePopuWindow sharePopuWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        view= LayoutInflater.from(YouYaLevelWebView.this).inflate(R.layout.webview_youya_level,null);
        setContentView(view);
        ShareSDK.initSDK(this);
        ButterKnife.inject(this);
        initFindView();
        initData();
        initOnClck();
    }

    @Override
    public void initOnClck() {
        titleToobar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CookieUtils.getInstence().actionKey(KeyEvent.KEYCODE_BACK);
            }
        });
    }

    @Override
    public void initFindView() {
        titleContent.setText("雅值");
        titleToobar.setNavigationIcon(R.mipmap.back);

    }

    @Override
    public void initData() {
        String url= MyUrl.LEVEL;
        webview.getSettings().setJavaScriptEnabled(true); ///------- 设置javascript 可用
        JavaScriptInterface JSInterface = new JavaScriptInterface(sharePopuWindow,this); ////------
        webview.addJavascriptInterface(JSInterface, "JSInterface"); // 设置js接口  第一个参数事件接口实例，第二个是实例在js中的别名，这个在js中会用到
        CookieUtils.getInstence().nullCookies(YouYaLevelWebView.this,webview,titleContent,url);

    }
    public class JavaScriptInterface {
        Activity activity;
        private sharePopuWindow share;

        public JavaScriptInterface(sharePopuWindow share,Activity activity) {
            this.share = share;
            this.activity=activity;
        }

        @JavascriptInterface
        public void changeActivity() {
            Log.e(MyUrl.TAG,"aaaaaa");
            /**
             * 分享
             */
            share=new sharePopuWindow(YouYaLevelWebView.this,MyUrl.JOIN, SharePreceUtils.getOther("user", "unique"));
            share.showAsDropDown(titleToobar, Gravity.CENTER,0,0);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
